package com.cct.digitalarchapi.controller;

import com.alibaba.fastjson2.JSONObject;
import com.cct.digitalarchapi.common.CommonUtil;
import com.cct.digitalarchapi.common.Response;;
import com.cct.digitalarchapi.pojo.DigitalArchMain;
import com.cct.digitalarchapi.service.DigitalArchMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * @FileName DigitalArchMainController
 * @Description 档案控制器
 * @Author Created by chenmingzhe
 * @Date 2023/11/19
 **/
@RestController
@RequestMapping(value = "/archMain")
public class DigitalArchMainController {
    @Autowired
    private DigitalArchMainService digitalArchMainService;
    @Autowired
    private CommonUtil commonUtil;

    /**
     * 新增档案
     */
    @RequestMapping(value = "/addArchMain",method = RequestMethod.POST)
    public Response addArchMain(@RequestBody DigitalArchMain digitalArchMain){
        commonUtil.recordOperateLog("新增档案标题为"+digitalArchMain.getArchTitle()+"的档案归档");
        return  digitalArchMainService.addArchMain(digitalArchMain);
    }
    /**
    *更新浏览和借阅次数
    */
    @RequestMapping(value = "/updateCount",method = RequestMethod.POST)
    public Response updateCount(@RequestBody JSONObject obj){
        //1：浏览次数＋1 2：下载次数+1
        String type=obj.getString("type");
        int archMainId=obj.getInteger("archMainId");
        if("1".equals(type)){
            commonUtil.recordOperateLog("浏览档案Id为"+archMainId+"的档案");
        }else if("2".equals(type)){
            commonUtil.recordOperateLog("借阅档案Id为"+archMainId+"的档案");
        }
        return  digitalArchMainService.updateCount(type,archMainId);
    }
    /**
    *查询档案
    */
    @RequestMapping(value = "/qryAllArchMain",method = RequestMethod.POST)
    public Response qryAllArchMain(@RequestBody JSONObject obj){
        int pageNum=obj.getInteger("pageNum");
        int pageSize=obj.getInteger("pageSize");
        return  digitalArchMainService.qryAllArchMain(pageNum,pageSize);
    }
    /**
    *根据档案Id查询档案详情
    */
    @RequestMapping(value = "/qryArchMainById",method = RequestMethod.POST)
    public Response qryArchMainById(@RequestBody JSONObject obj){
        int archMainId=obj.getInteger("archMainId");
        commonUtil.recordOperateLog("查询档案Id为"+archMainId+"的档案详情");
        return  digitalArchMainService.qryArchMainById(archMainId);
    }
    /**
     *根据类型查询所有档案
     */
    @RequestMapping(value = "/qryAllArchMainByType",method = RequestMethod.POST)
    public Response qryAllArchMainByType(@RequestBody JSONObject obj){
        int pageNum=obj.getInteger("pageNum");
        int pageSize=obj.getInteger("pageSize");
        //传-1代表所有
        int accessState= obj.getInteger("accessState");
        //传空代表所有
        String archState=obj.getString("archState");
        //传空代表所有
        String archCateCode=obj.getString("archCateCode");
        //传空代表所有
        String archDeptCode=obj.getString("archDeptCode");
        //传空代表所有
        String username=obj.getString("username");
        //传空代表所有
        String staffCode=obj.getString("staffCode");
        //传空代表所有,不为空则根据标题模糊搜索
        String archTitle=obj.getString("archTitle");
        //-1代表所有，精确查找，保管期
        int archPeriod=obj.getInteger("archPeriod");
        //浏览次数，大于传值次数，默认传0
        int visitCount=obj.getInteger("visitCount");
        //借阅次数，大于传值次数，默认传0
        int borrowCount=obj.getInteger("borrowCount");
        //传空代表所有,格式yyyy，年份
        String submitYear=obj.getString("submitYear");
        //传空代表所有,不为空则根据标题模糊搜索
        String archDeptHotName=obj.getString("archDeptHotName");
        return  digitalArchMainService.qryAllArchMainByType(pageNum,pageSize,accessState,
                archState,archCateCode,archDeptCode,username,staffCode,archTitle,archPeriod,visitCount,borrowCount,submitYear,archDeptHotName);
    }
    /**
    *上传文件
    */
    @RequestMapping(value="/uploadFile",method= RequestMethod.POST)
    public Response uploadFile(@RequestParam MultipartFile file,@RequestParam String archMainCode) {
        if (file.isEmpty()) {
            return Response.bad("文件为空");
        }
        commonUtil.recordOperateLog("上传了档案编码为"+archMainCode+"的附件，文件名为："+file.getOriginalFilename());
       return digitalArchMainService.uploadFile(file,archMainCode);
    }

    /**
    *获取宗号
    */
    @RequestMapping(value="/getSectList",method= RequestMethod.POST)
    public Response getSectList(@RequestBody JSONObject obj) {
        String archDeptCode=obj.getString("archDeptCode");
        String archCateCode=obj.getString("archCateCode");
        return digitalArchMainService.getSectList(archDeptCode,archCateCode);
    }
    /**
    *获取档案编号
    */
    @RequestMapping(value="/getArchMainCode",method= RequestMethod.POST)
    public Response getArchMainCode(@RequestBody JSONObject obj) {
        String archDeptCode=obj.getString("archDeptCode");
        String archCateCode=obj.getString("archCateCode");
        String sect=obj.getString("sect");
        String year=obj.getString("year");
        return digitalArchMainService.getArchMainCode(archDeptCode,archCateCode,sect,year);
    }
    /**
    *下载文件
    */
    @RequestMapping(value="/downLoadFile",method= RequestMethod.POST)
    public void downLoadFile(@RequestBody JSONObject obj,HttpServletResponse response){
        //文件Url
        String fileUrl=obj.getString("fileUrl");
        String archFilesId=obj.getString("archFilesId");
        commonUtil.recordOperateLog("下载了附件id为"+archFilesId+"的附件");
        digitalArchMainService.downLoadFile(fileUrl,response,archFilesId);
    }
    /**
    *更新档案状态
    */
    @RequestMapping(value="/updateArchMainState",method= RequestMethod.POST)
    public Response updateArchMainState(@RequestBody JSONObject obj) {
        //state
        int archState=obj.getInteger("archState");
        //archMainId
        int archMainId=obj.getInteger("archMainId");
        //操作人必送
        String auditUser=obj.getString("auditUser");
        commonUtil.recordOperateLog("更新了档案id为"+archMainId+"的档案状态为"+archState);
        return digitalArchMainService.updateArchMainState(archMainId,archState,auditUser);
    }
    /**
    *根据档案编码去查询附件
    */
    @RequestMapping(value="/queryFilesById",method= RequestMethod.POST)
    public Response queryFilesById(@RequestBody JSONObject obj) {
        //档案编码
        String archMainCode=obj.getString("archMainCode");
        //借阅id
        String recid=obj.getString("recid");
        return digitalArchMainService.queryFilesById(archMainCode,recid);
    }
    /**
    *根据档案类型查询已归档的档案数量
    */
    @RequestMapping(value="/getArchMainByCate",method= RequestMethod.POST)
    public Response getArchMainByCate() {
        return digitalArchMainService.getArchMainByCate();
    }
    /**
    *根据条件查看已提交的档案数量
    */
    @RequestMapping(value="/getArchMainByMonth",method= RequestMethod.POST)
    public Response getArchMainByMonth(@RequestBody JSONObject obj) {
        //传空为所有
        String archCateCode=obj.getString("archCateCode");
        //开始日期 yyyyMM,送空查当年年月
        String startDate=obj.getString("startDate");
        //结束日期 yyyMM，送空查当年年月
        String endDate=obj.getString("endDate");
        return digitalArchMainService.getArchMainByMonth(archCateCode,startDate,endDate);
    }

    /**
     *修改“档案名称”、“保管期”、“备注”
     */
    @RequestMapping(value="/updateArchMainInfo",method= RequestMethod.POST)
    public Response updateArchMainInfo(@RequestBody JSONObject obj) {
        //档案id
        int archMainId=obj.getInteger("archMainId");
        //档案标题
        String archMainTitle=obj.getString("archMainTitle");
        //保管期
        int  archPeriod=obj.getInteger("archPeriod");
        //档案标题
        String notes=obj.getString("notes");
        commonUtil.recordOperateLog("更新了档案id为"+archMainId+"的档案,标题名："+archMainTitle+",保管期："+archPeriod+"月，备注："+notes);
        return digitalArchMainService.updateArchMainInfo(archMainId,archMainTitle,archPeriod,notes);
    }
    /**
     *查询归档档案（不查询本人提交的正在审批状态的档案）
     */
    @RequestMapping(value="/getArchMain",method= RequestMethod.POST)
    public Response getArchMain(@RequestBody JSONObject obj) {
        //档案标题
        String  archMainTitle=obj.getString("archMainTitle");
        //送操作人
        String username=obj.getString("username");
        //送-1查全部，其他查对应状态
        int archState=obj.getInteger("archState");
        int pageNum=obj.getInteger("pageNum");
        int pageSize=obj.getInteger("pageSize");
        return digitalArchMainService.getArchMain(archMainTitle,username,archState,pageNum,pageSize);
    }

    /**
     *查询对应标签的档案数量
     */
    @RequestMapping(value="/getTypeAndNum",method= RequestMethod.POST)
    public Response getTypeAndNum() {
        return digitalArchMainService.getTypeAndNum();
    }
    /**
    *根据标签查对应文档
    */
    @RequestMapping(value="/getArchMainByType",method= RequestMethod.POST)
    public Response getArchMainByType(@RequestBody JSONObject obj) {
        //1 热点标签  2 年份 3 档案分类名字
        String type=obj.getString("type");
        //送对应的标签，例如2024
        String typeName=obj.getString("typeName");
        int pageNum=obj.getInteger("pageNum");
        int pageSize=obj.getInteger("pageSize");
        return digitalArchMainService.getArchMainByType(type,typeName,pageSize,pageNum);
    }

    /**
     *根据标签查对应文档
     */
    @RequestMapping(value="/getArchMainByMuchType",method= RequestMethod.POST)
    public Response getArchMainByMuchType(@RequestBody JSONObject obj) {
        //热点标签,list格式
        List<String> hotPointList=obj.getList("hotPoint",String.class);
        //年份格式 ,list格式
        List<String> yearList=obj.getList("year",String.class);
        // 档案分类要求格式 ,list格式
        List<String> cateTypeList=obj.getList("cateType",String.class);
        String hotPoint="('noChoose')";
        String year="('noChoose')";
        String cateType="('noChoose')";
        if (hotPointList.size() > 0) {
            hotPoint=commonUtil.listToString(hotPointList);
        }
        if (yearList.size() > 0) {
            year=commonUtil.listToString(yearList);
        }
        if (cateTypeList.size() > 0) {
            cateType=commonUtil.listToString(cateTypeList);
        }
        int pageNum=obj.getInteger("pageNum");
        int pageSize=obj.getInteger("pageSize");
        return digitalArchMainService.getArchMainByMuchType(hotPoint,year,cateType,pageSize,pageNum);
    }
    /**
    *查询我的部门的档分类树
    */
    @RequestMapping(value="/qryMyDeptArchMain",method= RequestMethod.POST)
    public Response qryMyDeptArchMain(@RequestBody JSONObject obj) {
        //部门code 例如01
        String deptCode=obj.getString("deptCode");
        return digitalArchMainService.qryMyDeptArchMain(deptCode);
    }

    /**
     *查询我的部门的对应分类的档案
     */
    @RequestMapping(value="/qryMyDeptArchMainByType",method= RequestMethod.POST)
    public Response qryMyDeptArchMainByType(@RequestBody JSONObject obj) {
        //部门Code 例如01
        String deptCode=obj.getString("deptCode");
        //返回的年份 例如2024
        String year=obj.getString("year");
        //返回的热点标签 例如其他
        String hotPoint=obj.getString("hotPoint");
        //返回的档案分类 例如文书
        String cateName=obj.getString("cateName");
        int pageNum=obj.getInteger("pageNum");
        int pageSize=obj.getInteger("pageSize");
        //空或null 搜全部，其他：模糊搜索标题
        String archTitle=obj.getString("archTitle");
        return digitalArchMainService.qryMyDeptArchMainByType(deptCode,year,hotPoint,cateName,pageNum,pageSize,archTitle);
    }

}
